// $URL: http://mingw-lib.googlecode.com/svn/trunk/working/avcodec_to_widget_8/t_box_input_file.cpp $
// $Rev: 262 $
// $Author: akio.miyoshi $
// $Date:: 2010-07-23 07:46:54 +0000#$

#include "t_box_input_file.h"

T_Box_Input_File::T_Box_Input_File(QString a_filename)
    : m_buffer(new quint8[c_buffer_size])
    , m_file(a_filename)
{
    if(!m_file.open(QIODevice::ReadOnly))
    {
        qDebug() << "file open failed";
    }
}

T_Box_Input_File::~T_Box_Input_File()
{
    if(m_file.isOpen()) m_file.close();
    delete [] m_buffer;
}

bool T_Box_Input_File::isValid()
{
    if(!m_file.isOpen()) return false;
    return true;
}

void T_Box_Input_File::close()
{
    if(!m_file.isOpen()) return;
    m_file.close();
}

quint8* T_Box_Input_File::getIOBuffer()
{
    return m_buffer;
}

int T_Box_Input_File::getIOBufferSize()
{
    return c_buffer_size;
}

int T_Box_Input_File::read_bytes(uint8_t *buf, int buf_size)
{
    if(!m_file.isOpen()) return 0;
    return m_file.read((char *)buf, buf_size);
}

int64_t T_Box_Input_File::seek_getpos()
{
    if(!m_file.isOpen()) return -1;
    return m_file.pos();
}

int64_t T_Box_Input_File::seek_setpos(int64_t offset)
{
    if(!m_file.isOpen()) return -1;
    if(!m_file.seek(offset))
    {
        qDebug() << "(!m_file.seek(offset))";
        return -1;
    }
    else
    {
        return m_file.pos();
    }
}

int64_t T_Box_Input_File::seek_length()
{
    if(!m_file.isOpen()) return -1;
    return m_file.size();
}
